//
//  TransparentNavigationBarVC.swift
//  elarc01
//
//  Created by elarc on 16/6/6.
//  Copyright © 2016年 elarc. All rights reserved.
//

import UIKit

class TransparentNavigationBarVC: NSObject {
    
    var barColor:UIColor?
    var tintColor:UIColor?
    var backgroundImage:UIImage?
    var statusBarStyle:UIStatusBarStyle?
    
    var zeroAlphaOffset:CGFloat?
    var fullAlphaOffset:CGFloat?
    var minAlphaValue:CGFloat?
    var maxAlphaValue:CGFloat?
    
    var fullAlphaTintColor:UIColor?
    var fullAlphaBarStyle:UIStatusBarStyle?
    
    var allChange:Bool?
    var reversal:Bool?
    var continues:Bool?
    
    private var backgroundImageView:UIImageView?
    private var selfNavigationBar:UINavigationBar?
    
    private var saveImage:UIImage?
    private var saveColor:UIColor?
    private var saveTintColor:UIColor?
    private var saveTitleAttribute:Dictionary<String,AnyObject>?
    private var saveBarStyle:UIStatusBarStyle?
    
    private var setFull:Bool?
    private var setZero:Bool?
    private var setChange:Bool?

//    #pragma mark - private method
    
    class func sharedManager() -> TransparentNavigationBarVC {
     let sharedInstance = TransparentNavigationBarVC()
     initBaseData(sharedInstance)
     return sharedInstance
        
    }
    
    class func initBaseData(manager:TransparentNavigationBarVC) {
        manager.backgroundImageView = UIImageView()
        manager.backgroundImageView?.frame = CGRect(x: 0,y: -20,width: ScreenWidth,height: NavigationH)
        manager.backgroundImageView?.backgroundColor = UIColor.whiteColor()
        manager.maxAlphaValue = 1
        manager.minAlphaValue = 0
        manager.fullAlphaOffset = 200
        manager.zeroAlphaOffset = -NavigationH
        manager.setZero = true
        manager.setFull = true
        manager.allChange = true
        manager.continues = true
    }
    
    class func setBarColor(color:UIColor) -> Void {
        self.sharedManager().barColor = color
        self.sharedManager().backgroundImageView?.backgroundColor = color
    }
    
    class func setTintColor(color:UIColor) -> Void {
        self.sharedManager().tintColor = color
        self.sharedManager().selfNavigationBar?.backgroundColor = color
        setTitleColorWithColor(color)
    }
    
    class func setBackgroundImage(image:UIImage) -> Void {
        self.sharedManager().backgroundImageView?.image = image
    }
    
    class func setStatusBarStyle(style:UIStatusBarStyle) -> Void {
        self.sharedManager().statusBarStyle = style
        UIApplication.sharedApplication().setStatusBarStyle(style, animated: true)
    }
    
    class func setZeroAlphaOffset(offset:CGFloat) -> Void {
        self.sharedManager().zeroAlphaOffset = offset
    }
    
    class func setFullAlphaOffset(offset:CGFloat) -> Void {
         self.sharedManager().fullAlphaOffset = offset
    }
    
    class func setMinAlphaValue(var value:CGFloat) -> Void {
        value = value < 0 ? 0 : value;
        self.sharedManager().minAlphaValue = value
    }
    
    class func setMaxAlphaValue(var value:CGFloat) -> Void {
        value = value > 1 ? 1 : value;
        self.sharedManager().maxAlphaValue = value
    }
    
    class func setFullAlphaTintColor(color:UIColor) -> Void {
        self.sharedManager().fullAlphaTintColor = color
    }
    
    class func setFullAlphaBarStyle(style:UIStatusBarStyle) -> Void {
        self.sharedManager().fullAlphaBarStyle = style
    }
    
    class func setAllChange(allChange:Bool) -> Void {
        self.sharedManager().allChange = allChange
    }
    
    class func setReversal(reversal:Bool) -> Void {
        self.sharedManager().reversal = reversal
    }
    
    class func setContinues(continues:Bool) -> Void {
        self.sharedManager().continues = continues
    }
    
    class func setNavigationBarColorWithAlpha(alpha:CGFloat) -> Void {
        
        let manager = self.sharedManager()
        manager.backgroundImageView?.backgroundColor = manager.barColor?.colorWithAlphaComponent(alpha)
    }
    
    class func setUIStatusBarStyle(style:UIStatusBarStyle) -> Void {
        UIApplication.sharedApplication().setStatusBarStyle(style, animated: true)
    }
    
    class func setTitleColorWithColor(color:UIColor) -> Void {

        let navBarAttributesDictionary: [String: AnyObject]? = [
            NSForegroundColorAttributeName: color,
            NSFontAttributeName: UIFont.systemFontOfSize(14)
        ]
          self.sharedManager().selfNavigationBar?.titleTextAttributes = navBarAttributesDictionary
    }
    
 //pragma mark - Public Method
    class func managerWithController(viewController:UIViewController) -> Void {
     let navigationBar = viewController.navigationController?.navigationBar
        let imageView = navigationBar?.subviews.first as? UIImageView
        self.sharedManager().saveImage = imageView?.image
        self.sharedManager().backgroundImageView = imageView
        self.sharedManager().selfNavigationBar = navigationBar
        navigationBar?.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
        navigationBar?.shadowImage = UIImage()
    }
    
    class func changeAlphaWithCurrentOffset(currentOffset:CGFloat) -> Void {
        let manager = sharedManager()
        let currentAlpha = curretAlphaForOffset(currentOffset)
        
        if manager.barColor != UIColor.clearColor() {
            
            if manager.continues == false {
                if currentAlpha == manager.minAlphaValue {
                    setNavigationBarColorWithAlpha(manager.minAlphaValue!)
                }else if currentAlpha == manager.maxAlphaValue {
                    UIView.animateWithDuration(0.35, animations: { 
                        setNavigationBarColorWithAlpha(manager.maxAlphaValue!)
                    })
                    manager.setChange = true
                }else{
                    if manager.setChange == true {
                        UIView.animateWithDuration(0.35, animations: {
                            setNavigationBarColorWithAlpha(manager.minAlphaValue!)
                        })
                        manager.setChange = false
                    }
                }
            }else{
                setNavigationBarColorWithAlpha(currentAlpha)
            }
        }
        
        if manager.allChange == true {
            changeTintColorWithOffset(currentAlpha)
        }
    
    }
    
    class func saveWithController(viewController:UIViewController) -> Void {
        let manager = sharedManager()
        let navigationBar = viewController.navigationController?.navigationBar
        let imageView = navigationBar?.subviews.first as? UIImageView
        manager.saveImage = imageView?.image
        manager.saveColor = imageView?.backgroundColor
        manager.saveTintColor = navigationBar?.tintColor
        manager.saveTitleAttribute = navigationBar?.titleTextAttributes
        manager.saveBarStyle = UIApplication.sharedApplication().statusBarStyle
    }
    
    class func reStoreWithZeroStatus() -> Void {
        changeAlphaWithCurrentOffset(self.sharedManager().zeroAlphaOffset!)
    }
    
    class func reStoreWithFullStatus() -> Void {
        changeAlphaWithCurrentOffset(self.sharedManager().fullAlphaOffset!)
    }
    
    class func reStore() -> Void {
        let manager = self.sharedManager()
        manager.selfNavigationBar?.setBackgroundImage(manager.saveImage, forBarMetrics: UIBarMetrics.Default)
        manager.selfNavigationBar!.subviews.first!.backgroundColor = manager.saveColor
        manager.selfNavigationBar!.tintColor = manager.saveTintColor
        manager.selfNavigationBar!.titleTextAttributes = manager.saveTitleAttribute
       UIApplication.sharedApplication().setStatusBarStyle(manager.saveBarStyle!, animated: true)
    }
    
    //#pragma mark - calculation
    
    class func curretAlphaForOffset(offset:CGFloat) -> CGFloat {
        
        let manager = self.sharedManager()
        var currentAlpha = (offset - manager.zeroAlphaOffset!)/(manager.fullAlphaOffset! - manager.zeroAlphaOffset!)
        currentAlpha = (currentAlpha < manager.minAlphaValue! ? manager.minAlphaValue:(currentAlpha > manager.maxAlphaValue ? manager.maxAlphaValue :currentAlpha))!
        
        currentAlpha = manager.reversal == true ? (manager.maxAlphaValue! + manager.minAlphaValue! - currentAlpha) : currentAlpha
        
        return currentAlpha

    }
    
    class func changeTintColorWithOffset(currentAlpha:CGFloat) -> Void {
        
        let manager = self.sharedManager()
        
        if currentAlpha >= manager.maxAlphaValue && manager.fullAlphaTintColor != nil {
            
            if manager.setFull == true {
                manager.setFull = false
                manager.setZero = true
            }else{
                if manager.reversal == true {
                    manager.setFull = true
                }
                return
            }
            
            manager.selfNavigationBar?.tintColor = manager.fullAlphaTintColor
            setTitleColorWithColor(manager.fullAlphaTintColor!)
            setUIStatusBarStyle(manager.fullAlphaBarStyle!)
        }else if (manager.tintColor != nil){
            if manager.setZero == true {
                manager.setZero = false
                manager.setFull = true
            }else{
                return
            }
            manager.selfNavigationBar?.tintColor = manager.tintColor
            setTitleColorWithColor(manager.tintColor!)
            setUIStatusBarStyle(manager.statusBarStyle!)
        }
        
    }
    
}
